/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2010 huliqing, huliqing.cn@gmail.com
 *
 * This file is part of QFaces.
 * QFaces is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QFaces is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with QFaces.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 这个文件是QFaces的一部分。
 * 您可以单独使用或分发这个文件，但请不要移除这个头部声明信息.
 * QFaces是一个自由软件，您可以自由分发、修改其中的源代码或者重新发布它，
 * 新的任何修改后的重新发布版必须同样在遵守LGPL3或更后续的版本协议下发布.
 * 关于LGPL协议的细则请参考COPYING、COPYING.LESSER文件，
 * 您可以在QFaces的相关目录中获得LGPL协议的副本，
 * 如果没有找到，请连接到 http://www.gnu.org/licenses/ 查看并获取副本。
 *
 * - Author: Huliqing
 * - Contact: huliqing.cn@gmail.com
 * - License: GNU Lesser General Public License (LGPL)
 * - Blog and source code availability: http://www.huliqing.name/
 */

package name.huliqing.qfaces.component;

import java.io.IOException;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import name.huliqing.qfaces.QFaces;

public class Debug extends UIComponentBase {
	@Override
	public String getFamily() {
		return null;
	}
	
	// 时间戳样式
	private String styleClassTime;
	
	// 输出信息的样式
	private String styleClassInfo;
	
	// 基本样式
	private String styleClass;
	
	// debug窗口背景图片
	private String bgSrc;
	
	// 宽度
	private Integer width;
	
	// 高度
	private Integer height;
	
	// 限制多少条记录,超过该记录之后将清除面板
	private Integer limit;
	
	// ---- 内部参数
	
	// 组件客户端ID，同时也是Frame ID
	private String ID_CLIENT;
	
	// 信息输出窗口ID
	private String ID_PANEL;
	
	@Override
    public void encodeBegin(FacesContext fc) throws IOException {
		
		// ---- 获取参数
		this.styleClassTime = (String) QFaces.getAttribute(this, "styleClassTime", "_qf_08_time");
		this.styleClassInfo = (String) QFaces.getAttribute(this, "styleClassInfo", "_qf_08_info");
		this.styleClass = (String) QFaces.getAttribute(this, "styleClass", "_qf_08_panel");
		this.bgSrc = (String) QFaces.getAttribute(this, "bgSrc", 
				QFaces.getResLoader2().makeImageSrc("08-3-bg.gif"));
		this.width = QFaces.convertToInteger(QFaces.getAttribute(this, "width", 500));
		this.height = QFaces.convertToInteger(QFaces.getAttribute(this, "height", 250));
		this.limit = QFaces.convertToInteger(QFaces.getAttribute(this, "limit", 5000));
		
		// ---- 指定ID
		this.ID_CLIENT = getClientId(fc);
		this.ID_PANEL = this.ID_CLIENT + ":panel";
		
		// ---- Render

		// overflow：auto为自动，yes为有，no为无
		// overflow-x：横向滚动条
		// overflow-y：纵向滚动条
		
		// OVERFLOW-Y:auto;
		// LINE-HEIGHT:100%;
		// SCROLLBAR-FACE-COLOR:#ffffff;		滑块颜色
		// SCROLLBAR-HIGHLIGHT-COLOR:#ffffff;	高亮颜色
		// SCROLLBAR-SHADOW-COLOR:#919192; 		阴影颜色
		// SCROLLBAR-3DLIGHT-COLOR:#ffffff;		三维光线颜色
		// SCROLLBAR-ARROW-COLOR:#919192; 		箭头颜色
		// SCROLLBAR-TRACK-COLOR:#ffffff;	 	滑道颜色
		// SCROLLBAR-DARKSHADOW-COLOR:#ffffff;	暗影颜色
		// LETTER-SPACING:1pt;
		
		StringBuilder style = new StringBuilder()
		.append("OVERFLOW:auto;")
		.append("LINE-HEIGHT:1.2;")
		.append("PADDING:3px;")
		.append("WIDTH:" + width + "px;")
		.append("HEIGHT:" + height + "px;")
		.append("BACKGROUND-IMAGE:url(" + bgSrc + ");")
		;
		
		ResponseWriter rw = fc.getResponseWriter();

		Frame fr = new Frame();
		fr.loadAttributeFrom(this);
		fr.encodeBegin(fc);
			rw.startElement("div", this);
			rw.writeAttribute("id", this.ID_PANEL, null);
			rw.writeAttribute("name", this.ID_PANEL, null);
			rw.writeAttribute("style", style, null);
			rw.writeAttribute("class", this.styleClass, null);
			rw.endElement("div");
		fr.encodeEnd(fc);
		
		// ---- Init JS
		StringBuilder jsInit = new StringBuilder("Q.setDebugId(")
		.append(" \"" + this.ID_PANEL + "\"")
		.append(",\"" + this.styleClassTime + "\"")
		.append(",\"" + this.styleClassInfo + "\"")
		.append(",\"" + this.limit + "\"")
		.append(")");
		
		QFaces.getResLoader2().loadJavaScriptInit(this, jsInit.toString());
	}
}
